/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inscripciones.DAO;



import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Query;
import inscripciones.Utiles.Util;

/**
 *
 * @author Dianita
 */
public class ConsultasBD {

    List lista;

    public List buscarPOR2(String tabla, String columna1, String valor1, String columna2, String valor2) {
        lista = new ArrayList();
        String query = "Select p from " + tabla + " p where p." + columna1 + "=" + valor1 + " AND p." + columna2 + "=" + valor2;
        System.out.println(query);
        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return lista;
    }

    public List buscarPOR(String tabla, String columna, String valor) {
        lista = new ArrayList();
        String query = "Select p from " + tabla + " p where p." + columna + "=" + valor;
        System.out.println(query);
        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return lista;
    }

    public List buscarPOR_ORDER(String tabla, String columna, String valor, String order) {
        lista = new ArrayList();
        String query = "Select p from " + tabla + " p where p." + columna + "=" + valor + " ORDER BY p." + order;
        System.out.println(query);
        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return lista;
    }

    public List buscarExcepto(String tabla, String columna, String valor, String columna2, String valor2) {
        lista = new ArrayList();
        String query = "Select p from " + tabla + " p where p." + columna + "!=" + valor + " AND p." + columna2 + "=" + valor2;
        System.out.println(query);
        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return lista;
    }

    public List buscarTodos(String tabla) {
        lista = new ArrayList();
        String query = "Select p from " + tabla + " p ";

        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return lista;
    }
    
    
      public List buscarTodosConsulta(String consult) {
        lista = new ArrayList();
        String query = consult;

        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return lista;
    }



    public long ContadorRegistros(String tabla, String campo) {
        long nroRegistros;
        String query = "SELECT COUNT(tabla) FROM " + tabla + " tabla";
        try {
            Query consulta = Conexion.em.createQuery(query);
            nroRegistros = Long.parseLong(String.valueOf(consulta.getSingleResult()));
            return nroRegistros;
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }

    public long ContadorRegistrosPersonalizado(String consult) {
        long nroRegistros;
        String query = consult;
        try {
            Query consulta = Conexion.em.createQuery(query);
            nroRegistros = Long.parseLong(String.valueOf(consulta.getSingleResult()));
            return nroRegistros;
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }

    public List validarIntervaloFecha(String tabla, String fechamenor, String fechamayor, Date fecha, String eleccion) {


        lista = new ArrayList();
        String query = "SELECT jor from " + tabla + " AS jor WHERE UNIX_TIMESTAMP('" + new Util().formatDATE(fecha) + "') >= UNIX_TIMESTAMP(jor." + fechamenor
                + ") AND UNIX_TIMESTAMP('" + new Util().formatDATE(fecha) + "') <= UNIX_TIMESTAMP(jor." + fechamayor + ") AND jor.eleccion=" + eleccion;
        System.out.println(query);
        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return lista;

    }

    public List buscarLIKE(String tabla, String columna, String valor) {
        lista = new ArrayList();
        String query = "Select p from " + tabla + " p where p." + columna + " like '" + valor + "%'";

        try {
            Query consulta = Conexion.em.createQuery(query);
            lista = consulta.getResultList();
        } catch (Exception e) {
            System.out.println(" no se pudo realizar la consulta ");
            return null;
        }
        return lista;
    }
    
    public static void main(String[] args) {
        //ORDER BY DISTINCT(COUNT(v.voto))  DESC 
        System.out.println(new ConsultasBD().buscarPOR("Abono", "forma", "2"));
    }
            
}
